package applet;

import java.util.ArrayList;

import processing.core.PApplet;

public class FilterPanel{
	
	GuiElement backDrop;
	ArtistDragMenu artistList;
	CountryFilterMenu countryList;
	FilterButton filterSwitch;
	FilterButton sexButtonM, sexButtonF;
	FilterButton allAgeButton, allCountryButton, allSexButton, updateButton;
	FilterButton soloButton, multiButton, clearAllButton;
	FilterButton showSexAgeButton,showCountryButton;
	ArrayList<FilterButton> ageButtons;
	public int x, y, width, height;
	public boolean active = false;
	
	Integrator interpolators;
	float transparency;
	

	public FilterPanel(int fpx, int fpy, int fpwidth, int fpheight)
	{
		x = fpx;
		y = fpy;
		width = fpwidth;
		height = fpheight;
		
		GLOBAL.curAge = "20 s";
		GLOBAL.curAgeId = 2;
		GLOBAL.curCountryId = 222;
		
		interpolators = new Integrator();
		interpolators.value = (float)(y - 300);
		interpolators.attraction = 0.8f;
		interpolators.damping = 0.2f;
		interpolators.target(y+46);
		
		soloButton = new FilterButton();
		soloButton.x = fpx+20;
		soloButton.y = fpy;
		soloButton.setLabel("Single");
		soloButton.width = 90;
		soloButton.height = 30;
		soloButton.active = !GLOBAL.artistsCompare;

		
		multiButton = new FilterButton();
		multiButton.x = fpx+115;
		multiButton.y = fpy;
		multiButton.setLabel("Multi");
		multiButton.width = 90;
		multiButton.height = 30;
		multiButton.active = GLOBAL.artistsCompare;
		
		showSexAgeButton = new FilterButton();
		showSexAgeButton.x = fpx+20;
		showSexAgeButton.y = fpy+600;
		showSexAgeButton.setLabel("Age+Sex");
		showSexAgeButton.width = 90;
		showSexAgeButton.height = 30;
		showSexAgeButton.active = !GLOBAL.showingCountry;

		
		showCountryButton = new FilterButton();
		showCountryButton.x = fpx+115;
		showCountryButton.y = fpy+600;
		showCountryButton.setLabel("Country");
		showCountryButton.width = 90;
		showCountryButton.height = 30;
		showCountryButton.active = GLOBAL.showingCountry;
		
		clearAllButton = new FilterButton();
		clearAllButton.x = fpx+20;
		clearAllButton.y = fpy+650;
		clearAllButton.setLabel("Clear All");
		clearAllButton.width = 185;
		clearAllButton.height = 30;
		
		backDrop = new GuiElement();
		backDrop.x = fpx+235;
		backDrop.y = (int)interpolators.value - 5;
		backDrop.width = fpwidth-230;
		backDrop.height = 410;
		backDrop.backColor = GLOBAL.processing.color(0);
		
		filterSwitch = new FilterButton();
		filterSwitch.x = fpx + 235;
		filterSwitch.y = fpy;
		//filterSwitch.setLabel("Sex:                     "+GLOBAL.curSex+"    Artist:                     Age:                     Date:                     ");
		filterSwitch.width = fpwidth-230;
		filterSwitch.height = 30;
		
		
		//
		sexButtonM = new FilterButton();
		sexButtonM.x = fpx + 250;
		sexButtonM.y = fpy + 50;
		sexButtonM.setLabel("");
		sexButtonM.setIcon(GLOBAL.baseDir + "artistm.png");
		sexButtonM.width = 190;
		sexButtonM.height = 190;
		
		sexButtonF = new FilterButton();
		sexButtonF.x = fpx + 250;
		sexButtonF.y = fpy + 260;
		sexButtonF.setLabel("");
		sexButtonF.setIcon(GLOBAL.baseDir + "artistf.png");
		sexButtonF.width = 190;
		sexButtonF.height = 190;
		
		sexButtonM.active = true;
		sexButtonF.active = true;
		
		artistList = new ArtistDragMenu(fpx+20, fpy+25, GLOBAL.items);
		artistList.dragDownSize =20;
		artistList.setLabel("");
		
		
		
		ageButtons = new ArrayList<FilterButton>();
		for(int i= 0; i<10; i++)
		{
			FilterButton a = new FilterButton();
			a.x = 520;
			a.y = 55 + i*40;
			a.width = 50;
			a.height = 38;
			a.setLabel(i*10+" s");
			if(i == 2)
				a.active = true;
			else
				a.active = false;
			ageButtons.add(a);
		}
		
		GLOBAL.countryButtons = new ArrayList<FilterButton>();
		
//Uncommented after v78~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//		for(int i= 0; i<250; i++)
//		{
//			FilterButton a = new FilterButton();
//			a.x = 550;
//			a.y = 55 + i*40;
//			a.width = 50;
//			a.height = 19;
//			a.setLabel(i+" Country");
//			a.id = i; //for GLOBAL use
//			if(i ==GLOBAL.curCountryId)
//				{
//					a.active = true;
//				}
//			else
//				a.active = false;
//			GLOBAL.countryButtons.add(a);
//		}
//End of uncommented~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
//Commented after v78/////////////////////////////////////////////////////////////////////////////
		GLOBAL.db.getAllCountries();
//Commented after v78/////////////////////////////////////////////////////////////////////////////

		countryList = new CountryFilterMenu(fpx+650, fpy+30, GLOBAL.countryButtons);
		countryList.dragDownSize =20;
		countryList.setLabel("");
		
		allAgeButton = new FilterButton();
		allAgeButton.x = x + 850;
		allAgeButton.y = y +50;
		allAgeButton.fixedSize = false;
		allAgeButton.setLabel("ALL AGES");
		allAgeButton.width = 100;
		
		allCountryButton = new FilterButton();
		allCountryButton.x = x +850;
		allCountryButton.y = y + 80;
		allCountryButton.setLabel("ALL NATIONS");
		allCountryButton.width = 100;
		
		allSexButton = new FilterButton();
		allSexButton.x = x +850;
		allSexButton.y = y + 110;
		allSexButton.setLabel("BOTH SEXES");
		allSexButton.width = 100;
		allSexButton.active = true;
		
		updateButton = new FilterButton();
		updateButton.x = x +850;
		updateButton.y = y + 380;
		updateButton.setLabel("UPDATE");
		updateButton.width = 100;
		
		
		if(GLOBAL.curCountryId<0)
			GLOBAL.curCountry = "ALL";
		else
			GLOBAL.curCountry = countryList.items.get(GLOBAL.curCountryId).label;

		GLOBAL.curSex = "Both";
		GLOBAL.prevSex = "Male";
	}
	
	public void draw()
	{
		//GLOBAL.processing.strokeWeight(1);
		//GLOBAL.processing.fill(GLOBAL.colorBackground);
		//GLOBAL.processing.textAlign(PApplet.LEFT, PApplet.LEFT);
		//GLOBAL.processing.textFont(GLOBAL.tFont,filterSwitch.fontSize);
		//GLOBAL.processing.text("[filterPanel-Place here only for a test]",filterSwitch.x, filterSwitch.y+35);

		
		artistList.y = y + 25;//(int)interpolators.value - 20;
		if(artistList.upButton!= null)
			artistList.upButton.y = artistList.y + artistList.height;
		if(artistList.downButton!=null && artistList.selectBar!= null)
			artistList.downButton.y = artistList.selectBar.y + artistList.selectBar.height;
		if (artistList.selectBar!= null)
			artistList.selectBar.y = artistList.y + artistList.height + 20;
		artistList.draw();
		
		soloButton.draw();
		multiButton.draw();
		
		if(GLOBAL.artistsCompare) {
			clearAllButton.draw();
			showCountryButton.draw();
			showSexAgeButton.draw();
		}
		
		if(!filterSwitch.active)
		{
			filterSwitch.label ="            Sex:                                   "+"    Age:                                "+"Country:                                                          ";
			filterSwitch.draw();
			GLOBAL.processing.textAlign(PApplet.LEFT, PApplet.LEFT);
			GLOBAL.processing.text(GLOBAL.curSex,filterSwitch.x+130,filterSwitch.y+21);
			//if(GLOBAL.curAgeId<0)
			//	GLOBAL.curAge = "ALL";
			GLOBAL.processing.text(GLOBAL.curAge, filterSwitch.x+310,filterSwitch.y+21);
			if(GLOBAL.curCountryId<0)
			{
				//GLOBAL.curCountry = "ALL";
				GLOBAL.processing.text(GLOBAL.curCountry,filterSwitch.x+520,filterSwitch.y+21);
			}
			else
				GLOBAL.processing.text(countryList.items.get(GLOBAL.curCountryId).label,filterSwitch.x+480,filterSwitch.y+21);
			return;	
		}
		
		if(filterSwitch.active)/**/
		{
			interpolators.update();
			transparency = PApplet.map(interpolators.value, (float)y-300, interpolators.target, 0, 255);
			
			backDrop.alphaVal = (int)transparency;
			backDrop.y = (int)interpolators.value-5;
			backDrop.height = 410;
			backDrop.draw();
			
			sexButtonM.alphaVal = transparency;
			sexButtonF.alphaVal = transparency;
			sexButtonM.y = (int)interpolators.value;
			sexButtonF.y = (int)interpolators.value + 210;
			
			countryList.y = (int)interpolators.value - 20;
			
			if(countryList.selectBar!=null)
				countryList.selectBar.y = countryList.y + countryList.height + 20;	
			if(countryList.upButton!=null)
				countryList.upButton.y = countryList.y + countryList.height;
			if(countryList.downButton!=null && countryList.selectBar!=null)
				countryList.downButton.y = countryList.selectBar.y + countryList.selectBar.height;

			allAgeButton.y = (int)interpolators.value;
			allCountryButton.y = (int)interpolators.value + 30;
			allSexButton.y = (int)interpolators.value + 60;
			updateButton.y = (int)interpolators.value + 378;
			allAgeButton.alphaVal = transparency;
			allCountryButton.alphaVal = transparency;
			allSexButton.alphaVal = transparency;
			updateButton.alphaVal = transparency;
			
			sexButtonM.draw();
			sexButtonF.draw();
			
			countryList.alphaVal = transparency;
			countryList.draw();
			for(int i=0; i<10; i++)
			{
				ageButtons.get(i).alphaVal = transparency;
				ageButtons.get(i).y = (int)interpolators.value + i*40;
				ageButtons.get(i).draw();
			}
			
			allAgeButton.draw();
			allCountryButton.draw();
			allSexButton.draw();
			updateButton.draw();
		}
		
		filterSwitch.label ="            Sex:                                   "+"    Age:                                "+"Country:                                                          ";
		filterSwitch.draw();
		GLOBAL.processing.textAlign(PApplet.LEFT, PApplet.LEFT);
		GLOBAL.processing.text(GLOBAL.curSex,filterSwitch.x+130,filterSwitch.y+21);
		//if(GLOBAL.curAgeId<0)
		//	GLOBAL.curAge = "ALL";
		GLOBAL.processing.text(GLOBAL.curAge, filterSwitch.x+310,filterSwitch.y+21);
		if(GLOBAL.curCountryId<0)
		{
			//GLOBAL.curCountry = "ALL";
			GLOBAL.processing.text(GLOBAL.curCountry,filterSwitch.x+520,filterSwitch.y+21);
		}
		else
			GLOBAL.processing.text(countryList.items.get(GLOBAL.curCountryId).label,filterSwitch.x+480,filterSwitch.y+21);

	}
	
	public void mouseRelease()
	{
		if(filterSwitch.mouseOver())
			filterSwitch.mouseReleased();
		artistList.mouseReleased();

		
		if(soloButton.mouseOver()) {
			GLOBAL.artistsCompare = false;
			soloButton.active = !GLOBAL.artistsCompare;
			multiButton.active = GLOBAL.artistsCompare;
		}

		if(multiButton.mouseOver()) {
			GLOBAL.artistsCompare = true;
			soloButton.active = !GLOBAL.artistsCompare;
			multiButton.active = GLOBAL.artistsCompare;
		}
		
		if(GLOBAL.artistsCompare && clearAllButton.mouseOver()) {
			GLOBAL.artistsCompareList.clear();
			for(int i = 0; i<4; i++) {
				GLOBAL.block[i].visible = false;
			}
		}
		
		if(GLOBAL.artistsCompare && showCountryButton.mouseOver()) {
			GLOBAL.showingCountry = true;
			showCountryButton.active = GLOBAL.showingCountry;
			showSexAgeButton.active = !GLOBAL.showingCountry;
		}
		if(GLOBAL.artistsCompare && showSexAgeButton.mouseOver()) {
			GLOBAL.showingCountry = false;
			showCountryButton.active = GLOBAL.showingCountry;
			showSexAgeButton.active = !GLOBAL.showingCountry;
		}
		
		if(filterSwitch.active) {
			
			//if(countryList.mouseOver())
			countryList.mouseReleased();
			if(countryList.selectBar !=null)
				countryList.selectBar.mouseReleased();
		
			if(updateButton.mouseOver()) {
				//We should call the function to update the artist list here
				
	//Commented after v78/////////////////////////////////////////////////////////////////////////////
				GLOBAL.db.getArtistsPopularityByAgeGenderCountry(GLOBAL.curAgeId, GLOBAL.curSex, GLOBAL.curCountry, GLOBAL.dataStart, GLOBAL.dataLimit);
	//Commented after v78/////////////////////////////////////////////////////////////////////////////
				
			}
			
			if(allAgeButton.mouseOver())
			{
				allAgeButton.mouseReleased();
				if(allAgeButton.active)
				{
					GLOBAL.prevAgeId = GLOBAL.curAgeId;
					GLOBAL.curAge = "ALL";
					GLOBAL.curAgeId = -1;
					for(FilterButton a: ageButtons)
						a.active = true;
				}
				else
				{
					for(FilterButton a: ageButtons)
						a.active = false;
					ageButtons.get(GLOBAL.prevAgeId).active = true;
					GLOBAL.curAge = ageButtons.get(GLOBAL.prevAgeId).label;
					GLOBAL.curAgeId = GLOBAL.prevAgeId;
				}
			}
			
			if(allCountryButton.mouseOver())
			{
				allCountryButton.mouseReleased();
				if(allCountryButton.active)
				{
					GLOBAL.prevCountryId = GLOBAL.curCountryId;
					GLOBAL.curCountry = "ALL";
					GLOBAL.curCountryId = -1;
					for(FilterButton a: countryList.items)
						a.active = true;
				}
				else
				{
					for(FilterButton a: countryList.items)
						a.active = false;
					countryList.items.get(GLOBAL.prevCountryId).active = true;
					GLOBAL.curCountry = countryList.items.get(GLOBAL.prevCountryId).label;
					GLOBAL.curCountryId = GLOBAL.prevCountryId;
				}
			}
			
			if(allSexButton.mouseOver())
			{
				allSexButton.mouseReleased();
				if(allSexButton.active)
				{
					GLOBAL.prevSex = GLOBAL.curSex;
					GLOBAL.curSex = "Both";
	
					sexButtonM.active = true;
					sexButtonF.active = true;
					allSexButton.active = true;
				}
				else
				{
					if(GLOBAL.prevSex == "Male")
					{
						sexButtonM.active = true;
						sexButtonF.active = false;
						GLOBAL.curSex = "Male";
					}
					else if(GLOBAL.prevSex == "Female")
					{
						sexButtonF.active = true;
						sexButtonM.active = false;
						GLOBAL.curSex = "Female";
					}
				}
			}
			if(sexButtonM.mouseOver())
			{
				sexButtonM.mouseReleased();
				if(GLOBAL.curSex =="Both")
				{
					allSexButton.active = false;
				}
				GLOBAL.curSex = "Male";
				sexButtonM.active = true;
				sexButtonF.active = false;
			}
			else if(sexButtonF.mouseOver())
			{
				sexButtonF.mouseReleased();
				if(GLOBAL.curSex =="Both")
				{
					allSexButton.active = false;
				}
				GLOBAL.curSex = "Female";
				sexButtonF.active = true;
				sexButtonM.active = false;
			}
			
	
			
			for(int i=0; i<ageButtons.size(); i++)
			{
				if(ageButtons.get(i).mouseOver())
				{
					if(allAgeButton.active)
					{
						for(int j=0;j<ageButtons.size();j++)
						{
							ageButtons.get(j).active = false;
						}
						ageButtons.get(i).active = true;
						GLOBAL.curAge = ageButtons.get(i).label;
						GLOBAL.curAgeId = i;
						allAgeButton.active = false;
						break;
					}
					else
					{
						ageButtons.get(i).mouseReleased();
						if(ageButtons.get(i).active)
						{
							GLOBAL.curAgeId = i;
							GLOBAL.curAge = ageButtons.get(i).label;
							for(FilterButton f : ageButtons)
								if(f!=ageButtons.get(i))
									f.active = false;
							break;
						}
						else
						{
							ageButtons.get(i).active = true;
						}
					}
				}
			}
			
			for(int i=0; i<countryList.items.size(); i++)
			{
				if(countryList.items.get(i).mouseOver())
				{
					if(allCountryButton.active)
					{
						for(int j=0;j<countryList.items.size();j++)
						{
							countryList.items.get(j).active = false;
						}
						countryList.items.get(i).active = true;
						GLOBAL.curCountry = countryList.items.get(i).label;
						GLOBAL.curCountryId = i;
						allCountryButton.active = false;
						break;
					}
					else
					{
						//countryList.items.get(i).mouseReleased();
						if(countryList.items.get(i).active)
						{//GLOBAL.processing.ellipse(500f, 400f, 100f, 100f);
							GLOBAL.curCountryId = i;
							for(FilterButton f : countryList.items)
								if(f!=countryList.items.get(i))
									f.active = false;
							break;
						}
						else
						{countryList.items.get(i).active = true;}
					}
				}
			}
		}

		if(filterSwitch.active && filterSwitch.mouseOver())
		{
			interpolators.value = (float)(y-300);
		}

	}

}
